/**
 * 
 */
package com.csc.bankingsystem.web.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.csc.bankingsystem.core.dto.BSAPIReturnObject;
import com.csc.bankingsystem.web.exception.BSAPIResponseException;

/**
 * Return object for Error in Controller
 * @author angelkid
 *
 */
@ControllerAdvice
public class BSExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler({ BSAPIResponseException.class })
    protected ResponseEntity<Object> handleInvalidRequest(RuntimeException e, WebRequest request) {
        BSAPIResponseException ire = (BSAPIResponseException) e;
        BSAPIReturnObject returnObj = new BSAPIReturnObject(ire.getCode(),ire.getMsg());
        
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        return handleExceptionInternal(e, returnObj, headers, HttpStatus.INTERNAL_SERVER_ERROR, request);
    }

}